title: "Gender diversity analysis" author: A. McSween date: '2022-09-14'
# Packages
import numpy as np
import pandas as pd
import plotly.express as px # for graphs
# Import .csv files for plotting
df = pd.read_csv(r"./age_groups.csv")
# make bar graph
fig = px.bar(
df, # the data to use
x = "Age group", # xaxis value
y = "Percentage", # yaxis value
color = "Gender" # which values to plot
)
fig.show()
fig = px.bar(
df, # the data to use
x = "Age group", # xaxis value
y = "Percentage", # yaxis value
color = "Gender", # which values to plot
barmode = "group" # group instead of stack values
)
fig.show()
I want to remove the legend title. Here the axis font size and angle already looks good! One point for Python (or more honestly, for Plotly).
fig = px.bar(
df, # the data to use
x = "Age group", # xaxis value
y = "Percentage", # yaxis value
color = "Gender", # which values to plot
barmode = "group" # group instead of stack values
)
fig.update_layout(legend_title_text="", xaxis_title="")
fig.show()
Finally I want to add a title and a caption that notes the source of the data.
fig = px.bar(
df, # the data to use
x = "Age group", # xaxis value
y = "Percentage", # yaxis value
color = "Gender", # which values to plot
barmode = "group", # group instead of stack values
title = "Gender diversity by age group" # figure title
)
fig.update_layout(legend_title_text="", xaxis_title="")
fig.add_annotation(text="Source: www150.statcan.gc.ca/n1/daily-quotidien/220427/dq220427b-eng.htm",
xref="paper", yref="paper",
x=.5, y=-.3, showarrow=False)
fig.show()
Let's just flip it and inspect to see what it looks like.
fig = px.bar(
df, # the data to use
y = "Age group", # xaxis value
x = "Percentage", # yaxis value
color = "Gender", # which values to plot
barmode = "group", # group instead of stack values
title = "Gender diversity by age group",
orientation="h"
)
fig.update_layout(legend_title_text="", xaxis_title="")
fig.add_annotation(text="Source: www150.statcan.gc.ca/n1/daily-quotidien/220427/dq220427b-eng.htm",
xref="paper", yref="paper",
x=.5, y=-.3, showarrow=False)
fig.update_yaxes(autorange="reversed")
fig.show()